<?php


namespace app\services;


use app\models\AppointProduct;

class AppointProductService extends Services {
    protected $limit;
    protected $pageSize;
    protected $page;
    protected $orderBy = 'id DESC';

    protected $_Users;
    protected $_Products;
    protected $type = 3;

    /**
     * @return mixed
     */
    public function getLimit() {
        return $this->limit;
    }

    /**
     * @param mixed $limit
     */
    public function setLimit($limit) {
        $this->limit = $limit;
    }

    /**
     * @return int
     */
    public function getPageSize() {
        return $this->pageSize;
    }

    /**
     * @param int $pageSize
     */
    public function setPageSize($pageSize) {
        $this->pageSize = $pageSize;
    }

    /**
     * @return int
     */
    public function getPage() {
        return $this->page;
    }

    /**
     * @param int $page
     */
    public function setPage($page) {
        $this->page = $page;
    }

    /**
     * @return string
     */
    public function getOrderBy() {
        return $this->orderBy;
    }

    /**
     * @param string $orderBy
     */
    public function setOrderBy($orderBy) {
        $this->orderBy = $orderBy;
    }

    /**
     * @return mixed
     */
    public function getUsers() {
        return $this->_Users;
    }

    /**
     * @param mixed $Users
     */
    public function setUsers($Users): void {
        $this->_Users = $Users;
    }

    /**
     * @return mixed
     */
    public function getProducts() {
        return $this->_Products;
    }

    /**
     * @return int
     */
    public function getType(): int {
        return $this->type;
    }

    /**
     * @param int $type
     */
    public function setType(int $type): void {
        $this->type = $type;
    }

    /**
     * @param mixed $Product
     */
    public function setProducts($Products): void {
        $this->_Products = $Products;
    }

    public function getList() {
        return AppointProduct::find()
            ->where($this->Params)
            ->orderBy($this->orderBy)
            ->offset($this->page)
            ->limit($this->pageSize)
            ->all();
    }

    public function getCount() {
        return AppointProduct::find()
            ->where($this->Params)
            ->count();
    }

    public function getAll() {
        return AppointProduct::find()
            ->where($this->Params)
            ->orderBy('id DESC')
            ->all();
    }

    public function getOne() {
        return AppointProduct::findOne($this->Params);
    }

    public function addAppointProdcut() {
        $AppointProduct = new AppointProduct();
        $AppointProduct->user_id = $this->_Users->id;
        $AppointProduct->product_id = $this->_Products->id;
        $AppointProduct->date = date("Y-m-d 00:00:00");
        $AppointProduct->ctime = date("Y-m-d H:i:s");
        $AppointProduct->utime = date("Y-m-d H:i:s");
        $AppointProduct->energy_number = $this->_Products->energy;
        if ($AppointProduct->save()) {
            $this->on(self::ON_PAY_SUCCESS,
                [(new UsersService()), 'onPaySuccess'],
                [
                    'Users' => $this->_Users,
                    'product_energy' => $this->_Products->energy,
                ]);


            $this->on(self::ON_PAY_SUCCESS,
                [(new EnergyLogsService()), 'onPaySuccess'],
                [
                    'type' => $this->type,
                    'energy_num' => $this->_Products->energy,
                    'user_id' => $this->_Users->id,
                ]
            );
            if (empty($this->trigger(self::ON_PAY_SUCCESS)))
                return true;
            else
                return false;
        }
    }
}